package com.gitee.hljdrl.dsbridge.clean;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.ffsky.logger.L;
import com.gitee.hljdrl.dsbridge.DSWebView;
import com.gitee.hljdrl.dsbridge.R;
import com.gitee.hljdrl.dsbridge.basis.BasePresenter;
import com.gitee.hljdrl.dsbridge.basis.DSViewEvent;
import com.gitee.hljdrl.dsbridge.basis.WebViewEvent;
import com.gitee.hljdrl.dsbridge.client.DSWebChromeClient;
import com.gitee.hljdrl.dsbridge.client.DSWebViewClient;
import com.gitee.hljdrl.dsbridge.util.DSSettingUtil;

public class DSViewPresenter extends BasePresenter implements DSViewEvent {

    protected String TAG = "DSViewPresenter";
    protected int mLoadProgress;
    protected View mLoadView;
    protected View mErrorView;
    protected DSWebView mWebView;
    protected ProgressBar mLoadProgressBar;
    protected DSWebChromeClient mDSWebChromeClient;

    @Override
    public void initView() {
        mWebView = findViewById(R.id.webview);
        mLoadView = findViewById(R.id.progress_panel);
        mErrorView = findViewById(R.id.error_panel);
        mLoadProgressBar = findViewById(R.id.progress_load);
        mDSWebChromeClient = new DSWebChromeClient(this, mWebView);
        mWebView.setWebChromeClient(mDSWebChromeClient);
        mWebView.setWebViewClient(new DSWebViewClient(this));
        String url;
        Bundle bundle = getBundle();
        if (bundle != null) {
            url = bundle.getString(WebViewEvent.KEY_URL);
        } else {
            url = getActivity().getIntent().getStringExtra(WebViewEvent.KEY_URL);
        }
        if (!onWebViewSetting(mWebView)) {
            DSSettingUtil.settings(mWebView, bundle.getBoolean(WebViewEvent.OPTION_ZOOM_ENABLE, false));
        }
        L.i(TAG, "url-->", url);
        if (!TextUtils.isEmpty(url)) {
            showLoading();
            mWebView.loadUrl(url);
        }
    }

    protected boolean onWebViewSetting(WebView webView) {
        return false;
    }

    @Override
    public void initEvent() {

    }


    @Override
    public void showLoading() {
        if (mLoadView != null) {
            mLoadView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideLoading() {
        if (mLoadView != null) {
            mLoadView.setVisibility(View.GONE);
        }
    }

    @Override
    public void showError() {
        if (mErrorView != null) {
            mErrorView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideError() {
        if (mErrorView != null) {
            mErrorView.setVisibility(View.GONE);
        }
    }

    @Override
    public void setProgress(int progress) {
        mLoadProgress = progress;
        if (mLoadProgressBar != null) {
            mLoadProgressBar.setProgress(progress);
        }
    }

    @Override
    public boolean onBackPressed() {
        if (mDSWebChromeClient != null && mDSWebChromeClient.isFullScreen()) {
            mDSWebChromeClient.onHideCustomView();
            return true;
        }
        return super.onBackPressed();
    }
}
